<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Wap\Controller;

use Think\Controller;
use Com\DingdingAuth;
/**
 * 后台用户控制器
 * 
 * @author jry <598821125@qq.com>
 */
class ServerController extends WapController
{

    /**
     * 首页
     */
    public function index()
    {
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
    	$ding_id=$user_info['userid'];
    	$ding_pass=M('Employee')->where(array('ding_id'=>$ding_id))->getField('ding_pass');
    	if (!$ding_pass) {
    		$this->error('没有权限！');
    	}    	 
    	// 搜索
    	$keyword = I('keyword', '', 'string');
    	$condition = array('like','%' . $keyword . '%');
    	$map['server_name|ip'] = array($condition,$condition,'_multi' => true );
    	
    	// 获取所有用户
    	$data_list = D('Server')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, 10)->where($map)->order('id desc')->select();
    	$page = new \Common\Util\Page(D('Server')->where($map)->count(), 10);
    	
    	foreach ($data_list as $key => &$val) {
    		$customerid=$val['customer_id'];
    		$val['customer_id']=get_customer_info($val['customer_id'],'customer_name');
    		$val['kh_id']=$customerid;
    		//$val['customer_id']="<a href='".U('Home/Customer/customerinfo',array('id'=>$customerid))."' style='text-decoration:none'>".$val['customer_id']."</a>";                       
    		$tt['tablename']='server_system';			
    		$tt['fieldvalue']=$val['system'];			
    		$server_system=D('Descriptions')->where($tt)->find();			
    		$val['system']=$server_system['title'];
    		$tt1['tablename']='server_buyer';
    		$tt1['fieldvalue']=$val['supplier'];
    		$server_buyer=D('Descriptions')->where($tt1)->find();
    		$val['supplier']=$server_buyer['title'];
    		$val['s_time']?$val['s_time']=date('Y-m-d H:i:s',$val['s_time']):'';
    		$val['e_time']?$val['e_time']=date('Y-m-d H:i:s',$val['e_time']):'';
    		switch($val['place']){
    			case "1":
    				$val['place']="香港";
    				break;
    			case "2":
    				$val['place']="美国";
    				break;
    			case "3":
    				$val['place']="大陆";
    				break;
    		}
    	}
    	
    	
    	$this->assign('HTTP_HOST', C('DING_HTTP_HOST'));
    	$this->assign('page', $page->show());
    	$this->assign('data_list', $data_list);
    	$this->assign('user_info', $user_info);
    	$this->assign('meta_title', '服务器列表');
    	$this->display();
    
    }
    /**
     * 新增服务器
     * */
    public  function  addserver(){
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
    	    	
    	if(IS_POST){
    		//dump($_POST);exit;
    		
    		$_POST['s_time']=strtotime($_POST['s_time']);
    		$_POST['e_time']=strtotime($_POST['e_time']);
    		
    		//$_POST['c_user']=is_login();
    		$server=D('Server');
    		$data=$server->create();
    		if($data){
    			$id=$server->add();
    			if($id){
    				$this->success('新增成功',U('index'));
    			}else{
    				$this->error('新增失败');
    			}
    		}else{
    			$this->error($server->getError());
    		}
    	}else{
    		
    		$supplier=D('Descriptions')->where('tablename="server_buyer"')->field('fieldvalue as id,title')->select();
    		$system=D('Descriptions')->where('tablename="server_system"')->field('fieldvalue as id,title')->select();
    		$cus=M("Customer")->field("id,customer_name")->select();
    		foreach($cus as $k=>$v){
    			$cuss[$v['id']]=$v['customer_name'];
    		}
    		$place=array("1"=>"香港","2"=>"美国","3"=>"大陆");
    		
    		$this->assign('cuss', $cuss);
    		$this->assign('system', $system);
    		$this->assign('supplier', $supplier);
    		$this->assign('place', $place);
    		$this->assign('user_info', $user_info);
    		$this->assign('meta_title', '新增服务器');
    		$this->display();
    	}
    }
    /**
     *编辑服务器
     */
    public  function editserver($id){		
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
	    if(IS_POST){
	    	$server=D('Server');
	    	$_POST['s_time']=strtotime($_POST['s_time']);	
	    	$_POST['e_time']=strtotime($_POST['e_time']);
	    	$data=$server->create();
	    	if($data){
	    		$res=$server->save();
	    		if($res){
	    			$this->success('更新成功',U('index'));
	    		}else{
	    			$this->error('更新失败');
	    		}
	    	}else{
	    		$this->error($server->getError());
	    	}
	    }else{
	    	
	    	$supplier=D('Descriptions')->where('tablename="server_buyer"')->field('fieldvalue as id,title')->select();
	    	$system=D('Descriptions')->where('tablename="server_system"')->field('fieldvalue as id,title')->select();
	    	$info=M('Server')->find($id);
	    	$place=array("1"=>"香港","2"=>"美国","3"=>"大陆");								
	    	$cus=M("Customer")->field("id,customer_name")->select();				
	    	foreach($cus as $k=>$v){					
	    		$cuss[$v['id']]=$v['customer_name'];				
	    	}

	    	$this->assign('info', $info);
		    $this->assign('cuss', $cuss);
		    $this->assign('system', $system);
		    $this->assign('supplier', $supplier);
		    $this->assign('place', $place);
		    $this->assign('user_info', $user_info);
		    $this->assign('meta_title', '编辑服务器');
		    $this->display();
	    }
    }
    
    /**
     * 搜索客户
     */
    public function getCustomerAjax(){
    
    	$project_keyword = I('keyword');
    	if(!empty($project_keyword)){
    		$condition = array('like','%'.$project_keyword.'%');
    		$map['customer_name|customer_num|mobile'] = array($condition,$condition,$condition,'_multi'=>true);
    	}
    	
    	$list=M('Customer')->where($map)->order("id desc")->select();
    	foreach ($list as $k => $vo) {
    
    		$data .= "<option value ='" . $vo['id'] . "'>" . $vo['customer_name']."</option>";
    			
    	}
    	$this->ajaxReturn($data);
    }
    /**
     * 删除服务器
     */
//     public function delServerAjax(){
    
//     	$id = I('id');
//     	$re=M('Server')->where('id='.$id)->delete();
//     	if ($re) {
//     		$data['info']=1;
//     	}else{
//     		$data['info']=0;
//     	}
//     	$this->ajaxReturn($data);
//     }
    /**
     * 服务器详情页
     */
    public function serverinfo(){
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
    	
    	$id=I('id');
    	$val = M('Server')->where('id='.$id)->find();
    	
    	$customerid=$val['customer_id'];
    	$val['customer_id']=get_customer_info($val['customer_id'],'customer_name');
    	//$val['customer_id']="<a href='".U('customer/xq',array('id'=>$customerid))."' style='text-decoration:none'>".$val['customer_id']."</a>";                       
    	$tt['tablename']='server_system';			
    	$tt['fieldvalue']=$val['system'];			
    	$server_system=D('Descriptions')->where($tt)->find();			
    	$val['system']=$server_system['title'];
    	$tt1['tablename']='server_buyer';
    	$tt1['fieldvalue']=$val['supplier'];
    	$server_buyer=D('Descriptions')->where($tt1)->find();
    	$val['supplier']=$server_buyer['title'];
    	$val['s_time']?$val['s_time']=date('Y-m-d H:i:s',$val['s_time']):'';
    	$val['e_time']?$val['e_time']=date('Y-m-d H:i:s',$val['e_time']):'';
    	switch($val['place']){
    		case "1":
    			$val['place']="香港";
    			break;
    		case "2":
    			$val['place']="美国";
    			break;
    		case "3":
    			$val['place']="大陆";
    			break;
    	}
    	
    	$this->assign('val', $val);
    	$this->assign('meta_title', '服务器详情');
    	$this->display();
    }
    
    /**
     * 搜索服务器
     */
    public function getServerAjax(){
    
    	$project_keyword = I('keyword');
    	if(!empty($project_keyword)){
    		$condition = array('like','%'.$project_keyword.'%');
    		$map['server_name|ip'] = array($condition,$condition,'_multi'=>true);
    	}
    	 
    	$list=M('Server')->where($map)->order("id desc")->select();
    	foreach ($list as $k => $vo) {
    
    		$data .= "<option value ='" . $vo['id'] . "'>" . $vo['server_name']."</option>";
    		 
    	}
    	$this->ajaxReturn($data);
    }
    /**
     * 搜索主机
     */
    public function getMasterAjax(){
    
    	$project_keyword = I('keyword');
    	if(!empty($project_keyword)){
    		$condition = array('like','%'.$project_keyword.'%');
    		$map['name|domain'] = array($condition,$condition,'_multi'=>true);
    	}
    	 
    	$list=M('Master')->where($map)->order("id desc")->select();
    	foreach ($list as $k => $vo) {
    
    		$data .= "<option value ='" . $vo['id'] . "'>" . $vo['name']."</option>";
    		 
    	}
    	$this->ajaxReturn($data);
    }
    /**
     * 搜索数据库
     */
    public function getDatabaseAjax(){
    
    	$project_keyword = I('keyword');
    	if(!empty($project_keyword)){
    		$condition = array('like','%'.$project_keyword.'%');
    		$map['name|account'] = array($condition,$condition,'_multi'=>true);
    	}
    	 
    	$list=M('Database')->where($map)->order("id desc")->select();
    	foreach ($list as $k => $vo) {
    
    		$data .= "<option value ='" . $vo['id'] . "'>" . $vo['name']."</option>";
    		 
    	}
    	$this->ajaxReturn($data);
    }
    /**
     * 搜索网站
     */
    public function getWebsiteAjax(){
    
    	$project_keyword = I('keyword');
    	if(!empty($project_keyword)){
    		$condition = array('like','%'.$project_keyword.'%');
    		$map['name|domain'] = array($condition,$condition,'_multi'=>true);
    	}
    
    	$list=M('Website')->where($map)->order("id desc")->select();
    	foreach ($list as $k => $vo) {
    
    		$data .= "<option value ='" . $vo['id'] . "'>" . $vo['name']."</option>";
    		 
    	}
    	$this->ajaxReturn($data);
    }
    
    
}   
